﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	App
	@vertion: 	0.1
	@date:		28/11/07
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/


package com.conelmate.cak.screens
{
	import com.conelmate.util.Trace;
	import com.conelmate.cak.screens.Login;
	import com.conelmate.cak.screens.Lobby;
	import com.conelmate.cak.game.GameEngine;
	import flash.display.MovieClip;

	import flash.display.DisplayObject;	
	import flash.events.Event;	


	public class ScreenManager extends Trace
	{
		static internal var	_INSTANCE		:ScreenManager;		//instancia de esta clase (singleton)
		static	private var _PARENT			:DisplayObject;		//ref al root
		
		static	private var _MC				:MovieClip;			//movie contenedor
		static	private var _objLogin		:Login;				//ref a Login
		static	private var _objLobby		:Lobby;				//ref a Lobby
		static	private	var _objGameEngine	:GameEngine;		//ref a GameEngine
		

//_________________________________________________________________________________ Constructor		
		function ScreenManager(enforcer:SingletonEnforcer)
		{
			_isTrace = false;
			mTrace("#ScreenManager# constructor");
		}
		


//_________________________________________________________________________________ Public Methods

		/**
		 * singleton
		 * devolver una instancia de la clase
		 */	
		public static function getInstance():ScreenManager
		{
			if(ScreenManager._INSTANCE == null) ScreenManager._INSTANCE = new ScreenManager(new SingletonEnforcer());
			return ScreenManager._INSTANCE;
		}
		
		
		/**
		 * inicializar
		 */		
		public function init(parent:DisplayObject = null):void
		{
			mTrace("#ScreenManager# init");
			
			_PARENT = parent || _PARENT;
			_MC = new mcInit();
			_MC.mcLobby.visible = false;
			_PARENT.addChild(_MC);

			initLogin();
		}
		
		
		/**
		 * destruir objetos y variables
		 */	
		public function destroy():void
		{
			mTrace("#ScreenManager# destroy");
			
			if(_objLogin) destroyLogin();
			if(_objLobby) destroyLobby();
			if(_objGame)  destroyGame();
			
			ScreenManager._INSTANCE = null;			
		}
		
		
//_________________________________________________________________________________ Private Methods

//LOGIN ***************************************************************************

		/**
		 * iniciar el login
		 */		
		private function initLogin():void
		{
			_objLogin = Login.getInstance();
			_objLogin.addEventListener(Login.LOGGED, onLogged);
			_objLogin.addEventListener(Login.ERROR,	onError);
			_objLogin.init(_MC);
		}
		
		
		/**
		 * finalizar el login
		 */		
		private function destroyLogin():void
		{
			_objLogin.destroy();
			_objLogin = null;
		}
		
		
		/**
		 * al loguearse pasar al lobby
		 */		
		private function onLogged(e:Event):void
		{
			mTrace("#App# onConnected");
			initLobby(_objLogin.nick);
			destroyLogin();			
		}


		private function onError(e:Event):void
		{
			mTrace("#App# onClosed");
		//	finishApp();
		}
		


//LOBBY ***************************************************************************


		private function initLobby(p_strNick:String):void
		{
			_objLobby = Lobby.getInstance();
		//	_objLogin.addEventListener(Login.FAILED,	onFailed);
			_objLobby.init(_MC, p_strNick);
		}


		private function destroyLobby():void
		{
			_objLobby.destroy();
			_objLobby = null;
		}
		
		
//GAME ****************************************************************************


		private function initGame():void
		{
			_objGameEngine = GameEngine.getInstance();
			_objGameEngine.init(_PARENT);
		}


		private function destroyGame():void
		{
			_objGameEngine.destroy();
			_objGameEngine = null;
		}



	}
}

internal class SingletonEnforcer{}